package avriServer.command.avriServer;

import java.util.HashMap;

import avriServer.client.NonexistantWaiterException;
import avriServer.client.UnvalidWaiterException;
import avriServer.client.Waiter;
import avriServer.client.Worker;
import avriServer.command.Command;
import avriServer.command.WaiterBuilder;
import avriServer.json.JSONException;
import avriServer.json.JSONObject;
import avriServer.server.Dispatcher;
import avriServer.util.IdentificationException;

public class PingAnswer extends Command {

	public PingAnswer(JSONObject _jsonObject, HashMap<String, String> _attributes ) throws IdentificationException, NonexistantWaiterException {
		super(_jsonObject, _attributes);
	}

	@Override
	public void execute(Worker _worker) {
		String serverTime = this.getCommandAttribute("serverTime");
		long pingOtherClientValue = System.currentTimeMillis() - Long.parseLong(serverTime);		
		String pingerName = this.getCommandAttribute("pingerName");
		try {
			Dispatcher.pushToWaiter(pingerName, "avriServer.pong", this.getCommandAttribute("timeFromPinger"), this.getAttribute("name"), pingOtherClientValue);
		} catch (NonexistantWaiterException e) {
		}
	}
}
